<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . "/database/$database/database.inc.php";

header('Content-Type: text/plain');

global $myManager;

try {
  $hostname = HOST_NAME;
  $username = USER_NAME2;
  $password = PASSWORD2;
  $dbname = DATABASE_NAME2;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}


//$orm1 = classToOrm('produceselect');
try {
  $stmt = $myPdo->prepare("SELECT t.e_oid AS id, 
                                  im.createdDate AS createdDate, im.sentDate AS sentDate, im.receivedDate AS receivedDate, im.receiver AS receiver, im.address AS address, im.code AS code, im.company AS company, im.weight AS weight, im.comment AS comment, im.review AS review, 
                                  u.trackId AS user_trackId, u.account AS user_account, u.name AS user_name, u.email AS user_email, u.type AS user_type,
                                  tr.sourceId as sourceId, tr.targetId as targetId
                           FROM ns_ProduceTrack AS t LEFT JOIN ns__ez_relation_produceitemmail_producetrack AS pim ON t.e_oid = pim.oid_a
                                                     LEFT JOIN ns_ProduceItemMail AS im ON im.e_oid = pim.oid_b
                                                     LEFT JOIN ns__ez_relation_producetrack_produceuser AS tu ON (t.e_oid = tu.oid_a AND tu.var_a = 'who') 
                                                     LEFT JOIN ns_ProduceUser AS u ON u.e_oid = tu.oid_b
                                                     LEFT JOIN ns__ez_relation_producetrack_producetransition AS pt ON t.e_oid = pt.oid_a 
                                                     LEFT JOIN ns_ProduceTransition AS tr ON tr.e_oid = pt.oid_b
                          ");
  $stmt->execute();
  
  //$myManager->start_t();
  $myPdo->beginTransaction();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $track = $stmt->fetch(PDO::FETCH_OBJ);
    
    $sql = "";
    
    if (isset($track->createdDate) &&
        isset($track->sentDate) &&
        isset($track->receivedDate) &&
        isset($track->receiver) &&
        isset($track->address) &&
        isset($track->code) &&
        isset($track->company) &&
        isset($track->weight) &&
        isset($track->comment) &&
        isset($track->review)) {
      $sql .= (empty($sql) ? "" : ", ") . "createdDate = '" . $track->createdDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "sentDate = '" . $track->sentDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "receivedDate = '" . $track->receivedDate . "'";
      $sql .= (empty($sql) ? "" : ", ") . "receiver = '" . $track->receiver . "'";
      $sql .= (empty($sql) ? "" : ", ") . "address = '" . $track->address . "'";
      $sql .= (empty($sql) ? "" : ", ") . "code = '" . $track->code . "'";
      $sql .= (empty($sql) ? "" : ", ") . "company = '" . $track->company . "'";
      $sql .= (empty($sql) ? "" : ", ") . "weight = '" . $track->weight . "'";
      $sql .= (empty($sql) ? "" : ", ") . "comment = '" . $track->comment . "'";
      $sql .= (empty($sql) ? "" : ", ") . "review = '" . $track->review . "'";
    }
    if (isset($track->user_trackId) &&
        isset($track->user_account) &&
        isset($track->user_name) &&
        isset($track->user_email) &&
        isset($track->user_type)) {
      $sql .= (empty($sql) ? "" : ", ") . "user_trackId = '" . $track->user_trackId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_account = '" . $track->user_account . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_name = '" . $track->user_name . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_email = '" . $track->user_email . "'";
      $sql .= (empty($sql) ? "" : ", ") . "user_type = '" . $track->user_type . "'";
    }
    
    if (isset($track->sourceId) &&
        isset($track->targetId)) {
      $sql .= (empty($sql) ? "" : ", ") . "sourceId = '" . $track->sourceId . "'";
      $sql .= (empty($sql) ? "" : ", ") . "targetId = '" . $track->targetId . "'";
    }
    
    if (!empty($sql)) {
      $myPdo->exec("UPDATE ns_ProduceTrack SET $sql WHERE e_oid = $track->id");
    }
    $i++;
  }
  $myPdo->commit();
  
  echo json_encode(array());
} catch (PDOException $e) {
  $myPdo->rollBack();
  echo $e->getMessage();
}
?>